//
//  DDRTTextBtnViewController.m
//  DDRichTextDemo
//
//  Created by 张小伟 on 2021/9/18.
//

#import "DDRTTextBtnViewController.h"
#import "DDRTGroupItemManager.h"
#import "DDRTSelectedManager.h"
#import "DDRTHeader.h"
#import "DDRTGroupMemberModel.h"

@interface DDRTTextBtnViewController ()<UITextViewDelegate>

#pragma mark 个人聊天还是群组聊天相关
@property(nonatomic,strong)UIView *groupSuperView;
@property(nonatomic,strong)UITextView *groupTextView;
@property(nonatomic,strong)UIButton *groupAddBtn;

@property(nonatomic,strong)DDRTGroupItemManager *groupItemManager;
@property(nonatomic,strong)DDRTSelectedManager *selectedGroupManager;


@end

@implementation DDRTTextBtnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"富文本成员";
    [self initAllGroupSubViews];

    // Do any additional setup after loading the view.
}

#pragma mark - Private Methods

-(void)initAllGroupSubViews {
    CGFloat groupSuperViewH = 50;
    // 群组的话，先把上次的单例对象清空
    self.groupItemManager = nil;
    // 群组专用的View根据是群组类型才加入
    [self.view addSubview:self.groupSuperView];
//        self.groupSuperView.backgroundColor = [UIColor blueColor];
    self.groupSuperView.frame = CGRectMake(0, kNavBarHeight, kScreenWidth, groupSuperViewH);
    
    UIView *lineView = [[UIView alloc] init];
    [self.groupSuperView addSubview:self.groupTextView];
    [self.groupSuperView addSubview:self.groupAddBtn];
    [self.groupSuperView addSubview:lineView];
    
    [self.groupTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.groupSuperView).with.offset(10);
        make.right.equalTo(self.groupAddBtn.mas_left).with.offset(-10);
        make.centerY.equalTo(self.groupSuperView).with.offset(0);
        make.height.mas_equalTo(@40);
    }];
    
    [self.groupAddBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.groupSuperView.mas_right).with.offset(-10);
        make.centerY.equalTo(self.groupSuperView).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.groupSuperView).with.offset(10);
        make.right.equalTo(self.groupSuperView).with.offset(0);
        make.bottom.equalTo(self.groupSuperView).with.offset(-1);
        make.height.mas_equalTo(@1.0);
    }];
    
    self.groupItemManager.groupMemberArr = self.selectedGroupManager.selectedArr;
    [self.groupItemManager updateAllItemStateToNomal];
    self.groupTextView.attributedText = [self.groupItemManager getAttributeAllMemberStr];
    self.groupTextView.contentSize = CGSizeMake(kScreenWidth *2, 40);

    self.groupSuperView.backgroundColor = [UIColor redColor];
    self.groupTextView.backgroundColor = [UIColor lightGrayColor];

   
}

#pragma mark - Action Methods

-(void)groupAddBtnClick:(UIButton *)btn {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    DDLog(@"text = %@",text);
    if ([text isEqualToString:@"\n"]){

        return NO;
    }
    if ([text isEqualToString:@""]) { // 点击删除按钮
        DDLog(@"text 是空 range.location = %lu,range.length = %lu",(unsigned long)range.location,(unsigned long)range.length);
        
        [self.groupItemManager deleteSelectedGroupArr];
        self.groupTextView.attributedText = [self.groupItemManager getAttributeAllMemberStr];

    }
    else if ([text isEqualToString:@" "]) {
        DDLog(@"text 是空格");
    }
    return NO;

    return YES;
}



- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    DDLog(@"%lu ------ %lu",(unsigned long)characterRange.location,(unsigned long)characterRange.length);
    if (textView == self.groupTextView) {
        NSInteger location = [[NSNumber numberWithUnsignedInteger:characterRange.location] integerValue];
        NSInteger clickIndex = [self.groupItemManager indexOfGroupLoctaion:location];
        if (clickIndex < self.groupItemManager.groupMemberArr.count) {
            DDRTGroupMemberModel *model = self.groupItemManager.groupMemberArr[clickIndex];
            if (model.isClick == YES) {
                [self.groupItemManager cancleItemState:model];
            }
            else {
                [self.groupItemManager selectedItemState:model];
            }
            self.groupTextView.attributedText = [self.groupItemManager getAttributeAllMemberStr];
            
        }
        
    }
  
    return YES;
}

#pragma mark - Setter & Getter

-(DDRTGroupItemManager *)groupItemManager {
    if (!_groupItemManager) {
        _groupItemManager = [DDRTGroupItemManager shareGroupItemManagerInstance];
    }
    return _groupItemManager;
}

-(DDRTSelectedManager *)selectedGroupManager {
    if (!_selectedGroupManager) {
        _selectedGroupManager = [DDRTSelectedManager shareSelectedGroupManagerInstance];
    }
    return _selectedGroupManager;
}

-(UIView *)groupSuperView {
    if (!_groupSuperView) {
        _groupSuperView = [[UIView alloc] init];
    }
    return _groupSuperView;
}
-(UITextView *)groupTextView {
    if (!_groupTextView) {
        _groupTextView = [[UITextView alloc] init];
        _groupTextView.bounces = NO;
        _groupTextView.delegate = self;
        _groupTextView.linkTextAttributes = @{};
//        _groupTextView.editable = NO;
//        _groupTextView.attributedText
    }
    return _groupTextView;
}
-(UIButton *)groupAddBtn {
    if (!_groupAddBtn) {
        _groupAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_groupAddBtn setTitle:@"添加" forState:UIControlStateNormal];
        _groupAddBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_groupAddBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_groupAddBtn setImage:[UIImage imageNamed:@"share_normal"] forState:UIControlStateNormal];
        [_groupAddBtn addTarget:self action:@selector(groupAddBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _groupAddBtn;
}

@end
